package com.mall.order.biz.handler;

import com.mall.order.biz.context.AbsTransHandlerContext;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.mq.order.DelayOrderProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @Description: 利用mq发送延迟取消订单消息，30分钟后
 * @Author： ciggar
 * @Date: 2019-09-17 23:14
 **/
@Component
@Slf4j
public class SendMessageHandler extends AbstractTransHandler {

	@Autowired
	DelayOrderProducer delayOrderProducer;

	@Override
	public boolean isAsync() {
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		// TODO 学了消息队列后做
		// 发送订单超时取消的延迟消息
		CreateOrderContext createOrderContext = (CreateOrderContext) context;

		// 1.从context中获取订单id
		String orderId = createOrderContext.getOrderId();

		// 创建Provider对象，设置nameserver address，启动provider
//		DefaultMQProducer producer = new DefaultMQProducer("order_producer");
//		producer.setNamesrvAddr("127.0.0.1:9876");

		// 2.在消息体重封装订单id，发出延时消息
		return delayOrderProducer.sendOrderDelayMessage(orderId);
		//return true;
	}
}